# -*- coding:utf-8 -*-
import urllib
import requests
import json
import re
import datetime
import yaml
import MySQLdb
conn = MySQLdb.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='123456',
    db='rainbow',
    charset='utf8'
)
cur=conn.cursor()
headers={
'Host': 'data.weibo.com',
'Connection': 'keep-alive',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1471.813 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Referer': 'http://data.weibo.com/index/hotword?wname=%E9%9D%92%E6%98%A5',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
}
cookies={'Cookie':'SUB=_2AkMvxLeBf8NhqwJRmP4RymjhaYRzzQDEieKZmEZaJRMxHRl-yT83qmU5tRBxBSXonNGcVpIHDHrJagtoRQxVjQ..; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WhBSiTsZHOqdkx.99IYAgKS; SINAGLOBAL=6160565575529.795.1486371002366; _s_tentry=s.jiathis.com; Apache=5133223651404.429.1487908539994; ULV=1487908540132:4:4:1:5133223651404.429.1487908539994:1486980491796; WEB3_PHP-FPM_GD=d57ec8cf86a1832de61f85ca904c73c8; UOR=www.cnbeta.com,widget.weibo.com,www.baidu.com; PHPSESSID=fs8bmn1suu475r7fpee9r3b977; WBStorage=02e13baf68409715|undefined'}
class weibo():
    name = 'blog'
    start_urls=[]
    def __init__(self):
        keyword='三生三世十里桃花'
        keyword_list=['三生三世十里桃花','孤芳不自赏','真正男子汉','我们十七岁','我们的挑战','王牌对王牌','向往的生活']
        #进行两级的quote才可以
        for i in range(len(keyword_list)):
            self.key1=urllib.quote(keyword_list[i])
            self.key=urllib.quote(self.key1)
            url='http://data.weibo.com/index/ajax/contrast?key2=%s&key3=&key4=&key5=&key6=&_t=0&__rnd=1488187528966'% self.key
            content=requests.get(url,headers=headers,cookies=cookies)
            print (content.text)
            content1=json.loads(content.text)
            self.id=content1['data']['key2']['id'].encode('utf-8')
            self.word=content1['data']['key2']['word'].encode('utf-8')
            print (u'这是获取得到的wid')
            print (type(id))
            print (self.word.decode('utf-8'))
            url1='http://data.weibo.com/index/ajax/getdate?month=3'
            content2=requests.get(url1,headers=headers,cookies=cookies)
            mouth=json.loads(content2.text)
            self.edate=mouth['edate'].encode('utf-8')
            self.sdate=mouth['sdate'].encode('utf-8')
            self.qushi(keyword_list[i])
            # self.zone()
            # self.shuxing()
        print (type(self.edate))
    def qushi(self,keyword):
        Add_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        self.base_url='http://data.weibo.com/index/ajax/getchartdata?wid=%s&sdate=%s&edate=%s&__rnd=1488187529026'%(self.id,self.sdate,self.edate)
        content3 = requests.get(self.base_url, headers=headers, cookies=cookies)
        #print (content3.text)
        js_qushi=json.loads(content3.text)
        yd=js_qushi['yd']
        zt=js_qushi['zt']
        # print (yd)
        # print (zt)
        # print (len(yd))
        # print (len(zt))
        print (type(zt))
        day_key_list=[]
        value_list=[]
        for j in range(len(zt)-1):
            #print zt[j]
            # print type(zt[j])
            js_zt=json.dumps(zt[j])
            #print js_zt
            day_key=re.findall('"day_key": "(.*?)"',js_zt,re.S)
            #print day_key
            value=re.findall('"value": "(.*?)"',js_zt,re.S)
            #print value
            day_key_list.extend(day_key)
            value_list.extend(value)
        sql='insert into SinaIndex values(%s,%s,%s,%s,%s)'
        for k in range(len(day_key_list)):
            cur.execute(sql,(u'整体趋势',str(keyword),str(day_key_list[k]),str(value_list[k]),str(Add_time)))
            conn.commit()
            # print js_zt
            #day_key=js_zt['day_key']

    def zone(self):
        zone_url='http://data.weibo.com/index/ajax/keywordzone?type=notdefault&wid=%s&__rnd=1488434073629'%(self.id)
        zone_content=requests.get(zone_url,headers=headers,cookies=cookies)
        #print zone_content.text
        js_zone=json.loads(zone_content.text)
        zone=js_zone['zone']
        print (zone)

    def shuxing(self):
        shuxing_url='http://data.weibo.com/index/ajax/getattributealldata?data=%7B%22key2%22:%7B%22id%22:%22'+self.id+'%22,%22word%22:%22'+self.key1+'%22%7D%7D&item=all&__rnd=1489543367682'
        shuxing_content=requests.get(shuxing_url,headers=headers,cookies=cookies)
        print (shuxing_content.text)

weibo()
# x.qushi()
# x.zone()
# x.shuxing()